package com.qianxun.module.datasource1.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianxun.framework.common.util.AssertUtil;
import com.qianxun.framework.common.util.Md5Util;
import com.qianxun.framework.common.util.ObjectUtil;
import com.qianxun.module.datasource1.admin.dto.SysDataModifyLockDTO;
import com.qianxun.module.datasource1.admin.entity.SysDataModifyLock;
import com.qianxun.module.datasource1.admin.entity.SysUser;
import com.qianxun.module.datasource1.admin.mapper.SysDataModifyLockMapper;
import com.qianxun.module.datasource1.admin.service.SysDataModifyLockService;
import com.qianxun.module.base.exception.ServiceInterruptionException;
import com.qianxun.plugins.shiro.ShiroUtil;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 数据可修改锁 定义数据的可修改锁，当数据被设置有修改锁时，数据将不能被CRUD操作，如需操作，需要先进行解锁操作；这样做的目的是：1，可限制权限，指定有权限的管理员才能操作，2，在操作时，对上锁功能需要进行解锁操作，防止管理员误操作 服务实现类
 * </p>
 *
 * @author admin
 * @since 2019-12-02
 */
@Service
public class SysDataModifyLockServiceImpl extends ServiceImpl<SysDataModifyLockMapper, SysDataModifyLock> implements SysDataModifyLockService {

    @Override
    public void checkLocked(String dataKey) {
        SysDataModifyLock sysDataModifyLock = this.baseMapper.selectByDataKey(dataKey);
        AssertUtil.isNull(sysDataModifyLock , ServiceInterruptionException.fail(401, "数据已上锁，请解锁后操作"));
    }

    @Override
    public void removeByKey(SysDataModifyLockDTO query) {
        checkAdmin(query.getPassword());

        //验证是否与数据锁密码相同
        SysDataModifyLock old = this.baseMapper.selectByDataKey(query.getDataKey());
        if(!Md5Util.getInstance().saltValid(query.getPassword() ,old.getPassword())){
            throw ServiceInterruptionException.fail(400,"无权解锁：解锁还需系锁人");
        }

        this.baseMapper.deleteByDataKey(query.getDataKey());

    }

    @Override
    public void add(SysDataModifyLockDTO query) {
        SysDataModifyLock old = this.baseMapper.selectByDataKey(query.getDataKey());
        AssertUtil.isNull(old , ServiceInterruptionException.fail(400,"数据已上锁"));

        checkAdmin(query.getPassword());

        SysDataModifyLock sysDataModifyLock = new SysDataModifyLock();
        ObjectUtil.switchAllFieldValue(sysDataModifyLock, query);
        sysDataModifyLock.init();

        //密码加密
        sysDataModifyLock.setPassword(Md5Util.getInstance().saltEncrypt(query.getPassword()));

        save(sysDataModifyLock);
    }

    @Override
    public SysDataModifyLock getByDataKey(String dataKey) {
        return this.baseMapper.selectByDataKey(dataKey);
    }

    @Override
    public void updatePasswordByCreateKey(String createKey, String newPassword) {
        this.baseMapper.updatePasswordByCreateKey(createKey ,newPassword);
    }

    private void checkAdmin(String password){
        SysUser sysUser = ShiroUtil.getUser(SysUser.class);;
        if(!Md5Util.getInstance().saltValid(password ,sysUser.getPassword())){
            throw ServiceInterruptionException.fail(400,"管理员密码错误");
        }
    }
}
